<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use webvimark\extensions\GridPageSize\GridPageSize;
use webvimark\modules\UserManagement\components\GhostHtml;
use webvimark\modules\UserManagement\models\rbacDB\Role;
use webvimark\modules\UserManagement\models\User;
use webvimark\modules\UserManagement\UserManagementModule;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $searchModel backend\models\UserSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = 'Users';
$this->params['breadcrumbs'][] = $this->title;
?>
<style type="text/css">
	/*.table  {
		margin-top: 6px;
	}
	.grid-view {
		position: relative;
		padding-bottom: 80px;
	}
    .pagination {
    	position: absolute;
    	left: 50%;
    	transform: translateX(-50%);
    }*/
</style>
<div class="user-index">
    <?php //echo $this->render('_search', ['model' => $searchModel]); ?>


    <div class="panel panel-default">
        <div class="panel-body">

            <div class="row">
                <div class="col-sm-6">
                    <p>
                        <?= Html::a('创建用户', ['create'], ['class' => 'btn btn-success']) ?>
                    </p>
                </div>

                <div class="col-sm-6 text-right">
                    <?= GridPageSize::widget(['pjaxId'=>'user-grid-pjax']) ?>
                </div>
            </div>
            <?php Pjax::begin([
                'id'=>'user-grid-pjax',
            ]) ?>
            <?= GridView::widget([
                'id'=>'user-grid',
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
                'pager'=>[
                    'options'=>['class'=>'pagination pagination-sm'],
                    'hideOnSinglePage'=>true,
                    'lastPageLabel'=>'>>',
                    'firstPageLabel'=>'<<',
                ],
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'label' => '头像',
                'format' => [
                    'image',
                    [
                        'width'=>'36',
                        'height'=>'36'
                    ]
                ],
                'value' => function ($model) {
                    return $model->avatar;
                }
            ],
            'username:text:登录名',
            'realname:text:真实姓名',
            [
                'label'=>'部门',
                'value'=>function(User $model){
                    $result = (new \yii\db\Query())
                        ->from('auth_item')
                        ->where(['name'=>$model->department])
                        ->one();
                   return  $result['description'];
                },
                'format'=>'raw',
            ],
            [
                'attribute'=>'gridRoleSearch',
                'filter'=>ArrayHelper::map(Role::getAvailableRoles(Yii::$app->user->isSuperAdmin),'name', 'description'),
                'value'=>function(User $model){
                    return implode(', ', ArrayHelper::map($model->roles, 'name', 'description'));
                },
                'format'=>'raw',
                'visible'=>User::hasPermission('viewUserRoles'),
            ],
            [
                'value'=>function(User $model){
                    return GhostHtml::a(
                        UserManagementModule::t('back', 'Roles and permissions'),
                        ['/user-management/user-permission/set', 'id'=>$model->id],
                        ['class'=>'btn btn-sm btn-primary', 'data-pjax'=>0]);
                },
                'format'=>'raw',
                'visible'=>User::canRoute('/user-management/user-permission/set'),
                'options'=>[
                    'width'=>'10px',
                ],
            ],
            [
                'value'=>function(User $model){
                    return GhostHtml::a(
                        UserManagementModule::t('back', 'Change password'),
                        ['change-password', 'id'=>$model->id],
                        ['class'=>'btn btn-sm btn-default', 'data-pjax'=>0]);
                },
                'format'=>'raw',
                'options'=>[
                    'width'=>'10px',
                ],
            ],
            [
                'class'=>'webvimark\components\StatusColumn',
                'attribute'=>'is_leader',
                'label'=>'是否是领导',
                'visible'=>[
                    [0, '否', 'success'],
                    [1, '是', 'warning'],
                ],
            ],
            ['class' => 'yii\grid\ActionColumn'],
        ],
    ]); ?>
    <?php Pjax::end(); ?>
        </div>
    </div>
</div>
        </div>
